package com.sduept.bigdata.iec61850.client;


import com.beanit.iec61850bean.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;

/**
 * @author laiy
 */
@RestController
@RequestMapping("/iec61850Client/")
public class ClientRestService {

	@Autowired
	private ClientService clientService;

	@GetMapping("/getDataSetValue")
	public String getDataSetValue(@RequestParam("serverAddress") String serverAddress,
								@RequestParam("reference") String reference) throws IOException, SclParseException {
		DataSet ds = clientService.getDataSetValues(serverAddress, reference);
		return  ds.toString();
	}

	@GetMapping("/getDataSets")
	public String getDataSets(@RequestParam("serverAddress") String serverAddress) {
		Collection<DataSet> ds = clientService.getDataSets(serverAddress);
		StringBuilder stringBuilder = new StringBuilder();
		for (DataSet d : ds) {
			stringBuilder.append(d.getReferenceStr());
			stringBuilder.append("\n");
		}
		return stringBuilder.toString();
	}

	@GetMapping("/findNode")
	public String findNode(@RequestParam("serverAddress") String serverAddress,
						 @RequestParam("reference") String reference,@RequestParam("fc") String fc) throws ServiceError, IOException {
		ModelNode node = clientService.findNode(serverAddress, reference,fc);
		return node.toString();
	}

	@GetMapping("/getFiles")
	public String getFiles(@RequestParam("serverAddress") String serverAddress,
						 @RequestParam("fileDirectory") String fileDirectory) throws ServiceError, IOException {
		List<FileInformation> fs = clientService.getFiles(serverAddress, fileDirectory);
		StringBuilder stringBuilder = new StringBuilder();
		for (FileInformation f : fs){
			stringBuilder.append(f.getFilename()+"|"+f.getFileSize()+"|"+f.getLastModified().toInstant().toString());
			stringBuilder.append("\n");
		}
		return stringBuilder.toString();
	}

	@GetMapping("/downloadFile")
	public void downloadFile(@RequestParam("serverAddress") String serverAddress,
							 @RequestParam("serverFilePathName") String serverFilePathName,
							 @RequestParam("localSavePath") String localSavePath) throws ServiceError, IOException {
		clientService.downloadFile(serverAddress, serverFilePathName, localSavePath);
	}
	@GetMapping("/getUrcbs")
	public void getUrcbs(@RequestParam("serverAddress") String serverAddress){
		Collection<Urcb> bs = clientService.getUrcbs(serverAddress);
		for (Urcb b : bs){
			System.out.println(b.getReference());
		}
	}
	@GetMapping("/getBrcbs")
	public String getBrcbs(@RequestParam("serverAddress") String serverAddress){
		Collection<Brcb> bs = clientService.getBrcbs(serverAddress);
		StringBuilder stringBuilder = new StringBuilder();
		for (Brcb b : bs){
			stringBuilder.append(b.getReference());
			stringBuilder.append("\n");
		}
		return stringBuilder.toString();
	}
	@GetMapping("/getUrcb")
	public String getUrcb(@RequestParam("serverAddress") String serverAddress,@RequestParam("reference") String reference){
		return clientService.getUrcb(serverAddress,reference).toString();
	}
	@GetMapping("/getBrcb")
	public String getBrcb(@RequestParam("serverAddress") String serverAddress,@RequestParam("reference") String reference){
		return  clientService.getBrcb(serverAddress,reference).toString();
	}
	@GetMapping("/enableBRcb")
	public void enableBRcb(@RequestParam("serverAddress") String serverAddress,@RequestParam("reference") String reference) throws ServiceError, IOException, SclParseException {
		clientService.enableBRcb(serverAddress,reference);
	}
	@GetMapping("/disableBRcb")
	public void disableURcb(@RequestParam("serverAddress") String serverAddress,@RequestParam("reference") String reference) throws ServiceError, IOException, SclParseException {
		clientService.disableBRcb(serverAddress,reference);
	}
	@GetMapping("/initServer")
	public void initServer(@RequestParam("serverAddress") String serverAddress) throws ServiceError, IOException, SclParseException {
		clientService.initServer();
	}
	@GetMapping("/setFloatValue")
	public void setFloatValue(@RequestParam("serverAddress") String serverAddress,@RequestParam("reference") String reference,@RequestParam("fc") String fc,@RequestParam("value") Float value) throws ServiceError, IOException, SclParseException {
		clientService.setDataValues(serverAddress,reference,fc,value);
	}
	@GetMapping("/setBooleanValue")
	public void setBooleanValue(@RequestParam("serverAddress") String serverAddress,@RequestParam("reference") String reference,@RequestParam("fc") String fc,@RequestParam("value") Boolean value) throws ServiceError, IOException, SclParseException {
		clientService.setDataValues(serverAddress,reference,fc,value);
	}
	@GetMapping("/setIntValue")
	public void setIntValue(@RequestParam("serverAddress") String serverAddress,@RequestParam("reference") String reference,@RequestParam("fc") String fc,@RequestParam("value") Integer value) throws ServiceError, IOException, SclParseException {
		clientService.setDataValues(serverAddress,reference,fc,value);
	}
	@GetMapping("/setLongValue")
	public void setLongValue(@RequestParam("serverAddress") String serverAddress,@RequestParam("reference") String reference,@RequestParam("fc") String fc,@RequestParam("value") Long value) throws ServiceError, IOException, SclParseException {
		clientService.setDataValues(serverAddress,reference,fc,value);
	}

	@GetMapping("/setStringValue")
	public void setStringValue(@RequestParam("serverAddress") String serverAddress,@RequestParam("reference") String reference,@RequestParam("fc") String fc,@RequestParam("value") String value) throws ServiceError, IOException, SclParseException {
		clientService.setDataValues(serverAddress,reference,fc,value.getBytes(StandardCharsets.UTF_8));
	}

	@GetMapping("/selectFcDataObject")
	public String selectActiveSG(@RequestParam("serverAddress") String serverAddress,@RequestParam("reference") String reference,@RequestParam("fc") String fc) throws ServiceError, IOException {
		return clientService.selectFcDataObject(serverAddress,reference,fc);
	}
	@GetMapping("/operate")
	public void operate(@RequestParam("serverAddress") String serverAddress,@RequestParam("reference") String reference,@RequestParam("fc") String fc) throws ServiceError, IOException {
		clientService.operate(serverAddress,reference,fc);
	}
}
